import { useRequest } from 'umi';
import { useMemo } from 'react';
import { getBaseUrl } from '@/services/urlconst';
import { getUserID, requestFunComponFiex } from '@/utils';

const baseUrl = getBaseUrl();

/** 获取采购变更申请单 */
export const GetPurchasingMethodChangeServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };

      let reqUrl = `${baseUrl}/purchaseSourceChangeController/getSourceChangeById/${bodyParams?.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    GetPurchasingMethodChangeRun: request?.run,
    GetPurchasingMethodChangeLoading: request?.loading,
    reloadDetailInfo: request?.refresh,
  };
};

/**
 * 查看当前流程进度
 * @controller 业务controller
 * @returns
 */
export const getImageByProcInstIDService = (callBack: (res: any) => void) => {
  const getImageByProcInstID = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/purchaseSourceChangeController/getImageByProcInstID/${params?.procInstID}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callBack,
    },
  );

  const data = useMemo(() => {
    return getImageByProcInstID?.data;
  }, [getImageByProcInstID?.data]);

  return {
    getImageByProcInstIDRun: getImageByProcInstID?.run,
    getImageByProcInstIDLoading: getImageByProcInstID?.loading,
    getImageByProcInstIDData: data,
  };
};

/**同步PDF*/
export const syncPDFtoOAServices = (cb: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}/purchaseSourceChangeController/syncWorkFlow`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    syncPDFtoOARun: request?.run,
    syncPDFtoOALoading: request?.loading,
  };
};
